﻿using AutoMapper;
using MediatR;
using MS.Data.API.Write.Applicant.Command;
using MS.Data.API.Write.Applicant.Command.User;
using MS.Data.Domain;
using MS.Data.ErrorCode;
using MS.Data.Infratructrue.Istanbul;

namespace MS.Data.API.Write.Applicant.CommandHandel.User
{
    public class XiuUserCommandHandler : IRequestHandler<XiuUserCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Domain.User> repository;
        private readonly IMapper mapper;

        public XiuUserCommandHandler(IBaseRepository<Domain.User> repository,IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public Task<APIResult<int>> Handle(XiuUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.code = APIEnum.更新成功;
            result.mes = APIEnum.更新成功.ToString();

            var updmodel = mapper.Map<Domain.User>(request);
            updmodel.UserId = request.UserId;
            var r = repository.Updates(updmodel);


            updmodel.UserId = request.UserId;
            updmodel.UserName = request.UserName;
            updmodel.UserPwd = request.UserPwd;
            updmodel.NickName = request.NickName;
            updmodel.RoleNames = request.RoleNames;


            if (r! > 0)
            {
                result.code = APIEnum.更新成功;
                result.mes = APIEnum.更新成功.ToString();
                result.data = r;
            }


            return Task.FromResult(result);
        }
    }
}
